package com.meta.act.app.system.repository.impl;

import com.meta.framework.util.BeanUtil;
import com.meta.act.app.system.mapper.TenantUserMapper;
import com.meta.act.app.system.repository.TenantUserRepository;
import com.meta.act.app.tenant.entity.TenantUser;
import com.meta.act.app.tenant.req.ro.TenantUserRo;
import com.meta.act.app.tenant.res.TenantUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 *  租户与用户关联表
 * @Title:      TenantUserRepositoryImpl
 * @Author:     Simon
 * @Remark:     The modified content
 * @CreateDate: 2020-11-04 15:23:04
 * @Version:    v2.0
 */
@Service
public class TenantUserRepositoryImpl implements TenantUserRepository {

	@Autowired
	private TenantUserMapper tenantUserMapper;

	/**
	 * insert tenantUser info
	 * @method      insertTenantUser
	 * @param       tenantUser:
	 * @return      void
	 * @author      Simon
	 * @date        2020-11-04 15:23:04
	 */
	@Override
	public int insertTenantUser(TenantUser tenantUser){
			return tenantUserMapper.insertTenantUser(tenantUser);
	}

	/**
	 * batch insert tenantUser info
	 * @method      insertTenantUser
	 * @param       tenantUsers:
	 * @return      void
	 * @author      Simon
	 * @date        2020-11-04 15:23:04
	 */
	@Override
	public void batchInsertTenantUser(List<TenantUser> tenantUsers){
			tenantUserMapper.batchInsertTenantUser(tenantUsers);
	}

	/**
	 * update tenantUser info
	 * @method      updateTenantUser
	 * @param       tenantUser:
	 * @return      int
	 * @author      Simon
	 * @date        2020-11-04 15:23:04
	 */
	@Override
	public int updateTenantUser(TenantUser tenantUser){
			return tenantUserMapper.updateTenantUser(tenantUser);
	}

	/**
	 * update tenantUser info by Pk
	 * @method      updateTenantUserByPk
	 * @param       tenantUser:
	 * @return      int
	 * @author      Simon
	 * @date        2020-11-04 15:23:04
	 */
	@Override
	public int updateTenantUserByPk(TenantUser tenantUser){
		return tenantUserMapper.updateTenantUserByPk(tenantUser);
	}

	/**
	 * list tenantUser info
	 * @method      listTenantUser
	 * @param       tenantUser:
	 * @return      List<TenantUser>
	 * @author      Simon
	 * @date        2020-11-04 15:23:04
	 */
	@Override
	public List<TenantUser> listTenantUser(TenantUser tenantUser){
			return tenantUserMapper.listTenantUser(BeanUtil.copyProperties(TenantUserRo.class,tenantUser));
	}

	@Override
	public List<TenantUserVo> listTenantUserInfo(TenantUser tenantUser){
		return tenantUserMapper.listTenantUserInfo(BeanUtil.copyProperties(TenantUserRo.class,tenantUser));
	}

	@Override
	public TenantUser getTenantUser(Long userId, Long tenantId){
		TenantUser tenantUser = new TenantUser();
		tenantUser.setUserId(userId);
		tenantUser.setTenantId(tenantId);

		List<TenantUser> tenantUsers = listTenantUser(tenantUser);
		if(CollectionUtils.isEmpty(tenantUsers)){
			return null;
		}
		return tenantUsers.get(0);
	};

}
